package com.lyz.core.impl;

import org.apache.log4j.Logger;

import com.lyz.config.ConfigFactory;
import com.lyz.config.IConfig;
import com.lyz.file.IFileFetcher;
import com.lyz.file.IFileProcess;
import com.lyz.file.IFileQueue;
import com.lyz.remote.IMsgHandler;
import com.lyz.util.PropertiesUtil;

/**
 * 获取各种实例
 * @author luyongzhao
 *
 */
public class FactoryService {
	
	private static final Logger logger = Logger.getLogger(FactoryService.class);
	
	private static IConfig config = ConfigFactory.getConfig();
	
	/**
	 * 获取文件获取器
	 * @param fileType
	 * @param queue
	 * @param tag
	 * @return 默认返回带有动态的时间
	 */
	public static IFileFetcher getFileFetcher(String className, IFileQueue queue, IFileProcess proc, String tag){
		return ((IFileFetcher)newInstance(className)).initFetcher(queue, proc, tag);
	}
	
	public static IMsgHandler getMsgHandler(String className, String tag, Context context){
		return ((IMsgHandler)newInstance(className)).initHandler(tag, context);
	}
	
	/**
	 * 反射机制实例化类
	 * @param className
	 * @return
	 */
	private static Object newInstance(String className){
		if(className == null){
			return null;
		}
		Class cls = null;
		Object obj = null;
		try {
			cls = Class.forName(className);
			obj = cls.newInstance();
		} catch (ClassNotFoundException e) {
			logger.error("class not found!",e);
			return null;
		} catch (IllegalAccessException e){
			logger.error(e);
		} catch (InstantiationException e){
			logger.error(e);
		}
		
		return obj;
	}
}
